package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.supermarket.biz.mapper.GsHomeManageMapper;
import com.sdy.supermarket.biz.model.GsHomeManage;
import com.sdy.supermarket.biz.model.GsHotRes;
import com.sdy.supermarket.biz.mapper.GsHotResMapper;
import com.sdy.supermarket.biz.model.GsRes;
import com.sdy.supermarket.biz.service.GsHotResService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2019-08-16
 */
@Slf4j
@Service
public class GsHotResServiceImpl extends BaseServiceImpl<GsHotRes> implements GsHotResService {
    @Resource
    private GsHotResMapper gsHotResMapper;
    @Resource
    private GsHomeManageMapper gsHomeManageMapper;


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int saveOrUpdateHotRes(GsHotRes hotRes) throws BizException {
        Date now = new Date();
        if (hotRes.getId() == null) {
            List<GsHotRes> list = gsHotResMapper.selectList(new LambdaQueryWrapper<>());
            hotRes.setSort(list.size() + 1).setCreateTime(now);
            if (hotRes.getOpenType() == null) hotRes.setOpenType(0);
            if (hotRes.getDisplayStatus() == null) hotRes.setDisplayStatus(0);
        }
        if (hotRes.getId() != null) {
            hotRes.setSort(null).setCreateTime(null);
        }
        boolean saveOrUpdate = saveOrUpdate(hotRes);
        int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(2).setLastUpdateTime(now));
        Assert.isTrue(!saveOrUpdate || homeUpdate == 0, "操作失败");
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int sortUpOrDown(Integer id, Integer type) throws BizException {
        GsHotRes hotRes = gsHotResMapper.selectById(id);
        LambdaQueryWrapper<GsHotRes> lambda = new LambdaQueryWrapper<>();
        if (type == 0) {
            lambda.orderByDesc(GsHotRes::getSort).lt(GsHotRes::getSort, hotRes.getSort());
        }
        if (type == 1) {
            lambda.orderByAsc(GsHotRes::getSort).gt(GsHotRes::getSort, hotRes.getSort());
        }
        List<GsHotRes> res = gsHotResMapper.selectList(lambda);
        if (res.size() > 0 && res.get(0) != null) {
            Date now = new Date();
            // 被动修改等级的数据
            GsHotRes down = new GsHotRes();
            down.setId(res.get(0).getId());
            down.setSort(hotRes.getSort());
            // 主动修改等级的数据
            GsHotRes up = new GsHotRes();
            up.setId(hotRes.getId());
            up.setSort(res.get(0).getSort());
            int downUpdate = gsHotResMapper.updateById(down);
            int upUpdate = gsHotResMapper.updateById(up);
            int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(2).setLastUpdateTime(now));
            if (downUpdate == 0 || upUpdate == 0 || homeUpdate == 0) {
                throw new BizException("修改失败");
            }
            return 1;
        }
        Assert.isTrue(true, "当前等级已经到达最低或者最高");
        return 0;
    }

    @Override
    public IPage<GsHotRes> pageList(Page<GsHotRes> page) {
        IPage<GsHotRes> iPage = page(page, Wrappers.<GsHotRes>lambdaQuery().orderByAsc(GsHotRes::getSort));
        return iPage;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteById(Integer id) throws BizException {
        Assert.isNull(id, "ID为NULL");
        GsHotRes gsRes = gsHotResMapper.selectById(id);
        LambdaQueryWrapper<GsHotRes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(GsHotRes::getSort, gsRes.getSort());
        List<GsHotRes> resList = gsHotResMapper.selectList(queryWrapper);
        int delete = gsHotResMapper.deleteById(id);
        if (resList.size() == 0) {
            return delete;
        }
        resList.forEach(res -> res.setSort(res.getSort() - 1));
        int batchUpdate = gsHotResMapper.batchUpdate(resList);
        int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(2).setLastUpdateTime(new Date()));
        if (delete == 0 || batchUpdate == 0 || homeUpdate == 0) {
            throw new BizException("删除失败");
        }
        return 1;
    }

}
